<?php

namespace App\Trip\Orders\BookOrders\Train\PriceDetails;

use App\Models\Setting\Setting;
use App\Models\Trip\TripOrderCosts\TripOrderCost;
use App\Trip\Orders\BookOrders\Train\Order\AbstractOrder\AbstractBuildOrderInfo;
use App\Trip\Orders\BookOrders\Train\PriceDetails\AbstractPriceDetails\AbstractPriceDetails;
use App\Trip\Orders\TripNo;
use App\Trip\Passengers\Passengers;
use Exception;
use GuzzleHttp\Exception\GuzzleException;

class BookPriceDetails extends AbstractPriceDetails
{
    private array                  $passengers;
    private int                    $trip_no;
    private AbstractBuildOrderInfo $orderInfo;
    private float                  $service_fee;
    private array                  $price_details;
    /**
     * @var float|int|mixed
     */
    private mixed $order_amount;

    /**
     * @throws GuzzleException
     */
    public function __construct(AbstractBuildOrderInfo $orderInfo)
    {
        $this->orderInfo   = $orderInfo;
        $this->passengers  = $orderInfo->passengers();
        $this->trip_no     = $orderInfo->getOrderParam()->getTripNo();
        $this->service_fee = $this->getServiceFee();
        $this->buildPriceDetails();
    }

    public function getServiceFee(): float
    {
        /**
         * 线上手续费 目前是  0
         */
        if ($this->orderInfo->getOrderParam()->getOnline() == 1) {
            return 0;
        }
        $setting = new Setting();
        return $setting->TrainOfflineBookServiceFee();
    }

    /**
     * @throws Exception
     * @throws GuzzleException
     */
    private function buildPriceDetails()
    {
        $passengers  = $this->passengers;
        $name        = TripNo::TRIP_NO_DESC[$this->trip_no];
        $details     = [];
        $total_price = 0;
        foreach ($passengers as $key => $passenger) {
            $passenger_type                      = $passenger['passenger_type'];
            $passenger_type_name                 = Passengers::PASSENGER_TYPE_DESC[$passenger_type];
            $passengers[$key]['service_fee']     = $this->service_fee;
            $passengers[$key]['price_details'][] = [
                "type"        => TripOrderCost::COST_TYPE_TICKET_SERVICE_FEE,
                "parent_type" => TripOrderCost::COST_TYPE_TICKET_SERVICE_FEE,
                "name"        => "购票手续费",
                "price"       => $this->service_fee,
            ];
            $passengers[$key]['price_details'][] = [
                "type"        => TripOrderCost::COST_TYPE_TICKET,
                "parent_type" => TripOrderCost::COST_TYPE_TICKET,
                "name"        => Passengers::PASSENGER_TYPE_DESC[$passenger_type],
                "price"       => $passengers[$key]['seat_price'],
            ];
            $ticket_price_key                    = "ticket_price:".$passenger_type_name;
            if (!isset($details[$ticket_price_key])) {
                $details[$ticket_price_key] = [
                    "type"        => TripOrderCost::COST_TYPE_TICKET,
                    "parent_type" => TripOrderCost::COST_TYPE_TICKET,
                    "name"        => $passenger_type_name,
                    "name_uy"     => trans2uy($passenger_type_name),
                    "price"       => $passenger['seat_price'],
                    "count"       => 1,
                ];
            } else {
                $details[$ticket_price_key]["count"]++;
            }
            $total_price         += $passenger['seat_price'];
            $ticket_service_key  = "ticket_service_fee";
            $ticket_service_type = TripOrderCost::COST_TYPE_TICKET_SERVICE_FEE;
            $ticket_service_name = "购票服务费";
            if (!isset($details[$ticket_service_key])) {
                $details[$ticket_service_key] = [
                    "type"        => $ticket_service_type,
                    "parent_type" => $ticket_service_type,
                    "name"        => $ticket_service_name,
                    "name_uy"     => trans2uy($ticket_service_name),
                    "price"       => $this->service_fee,
                    "count"       => 1,
                ];
            } else {
                $details[$ticket_service_key]["count"]++;
            }
            $total_price += $this->service_fee;

        }


        $select_seat_price = $this->orderInfo->getSelectedSeats()->getSelectSeatPrice();
        $select_seat_count = $this->orderInfo->getSelectedSeats()->getSeatsCount();
        $total_price       += $select_seat_price;
        if ($select_seat_price > 0) {
            $selected_seat_key           = "selected_seat_fee";
            $selected_seat_name          = "优选铺席";
            $details[$selected_seat_key] = [
                "type"        => TripOrderCost::COST_TYPE_SELECT_SEAT,
                "parent_type" => TripOrderCost::COST_TYPE_SELECT_SEAT,
                "name"        => $selected_seat_name,
                "name_uy"     => trans2uy($selected_seat_name),
                "price"       => $select_seat_count > 0 ? $select_seat_price / $select_seat_count : 0,
                "count"       => $select_seat_count,
            ];
        }
        $this->passengers    = $passengers;
        $this->price_details = [
            'name'        => $name,
            'name_uy'     => trans2uy($name),
            "total_price" => $total_price,
            'details'     => collect($details)->sortBy('type')->values()->toArray(),
        ];
        $this->order_amount  = $total_price;
    }

    public function getPriceDetails(): array
    {
        return $this->price_details;
    }


    /**
     * @inheritDoc
     */
    public function calcReceivable(): float
    {
        return $this->getOrderAmount();
    }

    function getOrderAmount(): float
    {
        return $this->order_amount;
    }

    /**
     * @inheritDoc
     */
    public function calcOriginNeedRefundAmount(): float
    {
        return 0;
    }

    public function getPassengers(): array
    {
        return $this->passengers;
    }
}